<?php

namespace Rblog\Module\Index\Controller;

class Comment extends \Rexzy\App\Controller {
    
    private $_art;
    private $_sort;
    private $_comment;
    private $_parentSort;
    
    public function __construct() {
        parent::__contruct();
        $this->_art = \Rexzy::model( 'Art' );
        $this->_sort = \Rexzy::model( 'Sort' );
        $this->_comment = \Rexzy::model( 'Comment' );
        $this->_parentSort = $this->_sort->navSort( 0 );
        $this->view->assign( array(
            'sorts' => $this->_parentSort,
            'sortUrl' => \Rexzy::url( 'sort/' ),
            'titleUrl' => \Rexzy::url( 'art/')
        ));
    }
    
    
    /**
     * 发表新评论
     */
    public function newcomment() {
        $post = $this->request->allInput( 'p' );
        
        $name = array(
            'val' => $post['name'],
            'message' => '请输入称呼名',
        );
        
        $email = array(
            'val' => $post['email'],
            'message' => '请输入邮箱地址',
        );
        
        $commentval = array(
            'val' => htmlspecialchars( $post['commentval'] ),
            'message' => '请输入评论内容',
        );
        $parent = $post['parent'];
        $artid = $post['artid'];
        
        $data = array(
            'name' => $name,
            'email' => $email,
            'commentval' => $commentval,
        );
        
        $add = function( $val )use( $parent, $artid ) {
            $val['parent'] = $parent;
            $val['artid'] = $artid;
            return $this->_comment->add( $val );
        };
        
        echo json_encode( $this->formCheck( $data, $add ) );
    }
}